package com.jye.rseven.user.controller;



import com.jye.rseven.Constants.MyConstants;
import com.jye.rseven.domain.Videos;
import com.jye.rseven.service.VideoService;
import com.jye.rseven.domain.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/video")
@Api(tags="视频控制器API")
public class VideosController {

    @Autowired
    VideoService videoService;

    @GetMapping("/findVideosByPId")
    @ApiOperation(value="查询视频", notes="根据父pId查找视频", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "pId",
                    value = "父ID", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findVideosByPId(int pId){
        JsonResult jsonResult = new JsonResult();
        //可看状态
        int status = MyConstants.videoStatus.available;
        List<Videos> videosList = videoService.findVideosByPIdAndStatus(pId,status);
        if(videosList.size()!=0){
            jsonResult.setData(videosList);
        }else{
            jsonResult.setCode("500");
            jsonResult.setMsg("查询失败!");
        }
        return jsonResult;
    }


    @GetMapping("/findVideoById")
    @ApiOperation(value="查询视频详情", notes="根据Id查找视频", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "id",
                    value = "id", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findVideoById(int id){
        JsonResult jsonResult = new JsonResult();
        Videos videoById = videoService.findVideoById(id);
        if(!StringUtils.isEmpty(videoById)){
            jsonResult.setData(videoById);
        }else{
            jsonResult.setCode("500");
            jsonResult.setMsg("查询失败!");
        }
        return jsonResult;
    }

    @PostMapping("/saveVideo")
    @ApiOperation(value="添加视频", notes="添加视频", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "Videos",
                    value = "Videos实体类", required = true,
                    dataType = "Integer"),
    })
    public JsonResult saveVideo(Videos videos){
        JsonResult jsonResult = new JsonResult();
        int i = videoService.saveVideo(videos);
        if(i==1){
            jsonResult.setMsg("添加成功!");
        }else{
            jsonResult.setCode("500");
            jsonResult.setMsg("添加失败!");
        }
        return jsonResult;
    }

    @PutMapping("/saveVideo")
    @ApiOperation(value="修改视频信息", notes="修改视频信息", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "Videos",
                    value = "Videos实体类", required = true,
                    dataType = "Integer"),
    })
    public JsonResult saveVideo2(Videos videos){
        JsonResult jsonResult = new JsonResult();
        int i = videoService.saveVideo(videos);
        if(i==1){
            jsonResult.setMsg("修改成功!");
        }else{
            jsonResult.setCode("500");
            jsonResult.setMsg("修改失败!");
        }
        return jsonResult;
    }

}
